	package com.dx.mes.service.brm.service;

	import static com.dx.mes.common.bean.def.GenericDef.*;

	import java.sql.Timestamp;
	import java.util.ArrayList;
	import java.util.List;

	import com.dx.common.table.ppt.*;
	import com.dx.mes.common.dao.IRetPrdInfoDao;
	import com.dx.mes.common.transcoder.util.Util;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.stereotype.Service;
	import org.springframework.transaction.interceptor.TransactionAspectSupport;

	import com.dx.mes.common.dao.IBisPathDao;
	import com.dx.mes.common.dao.IBisPathSwhDao;
	import com.dx.mes.common.dao.IBisPathiDao;
	import com.dx.mes.common.transcoder.util.AppContext;
	import com.dx.mes.common.transcoder.util.JacksonUtil;
	import com.dx.mes.common.transcoder.util.LogUtils;
	import com.dx.mes.facade.brm.service.IFbpswtpthService;
	import com.dx.mes.facade.brm.tx.Fbpswtpth.FbpswhpthI;
	import com.dx.mes.facade.brm.tx.Fbpswtpth.FbpswhpthIA;
	import com.dx.mes.facade.brm.tx.Fbpswtpth.FbpswhpthO;
	import com.dx.mes.facade.brm.tx.Fbpswtpth.FbpswhpthOA;

	@Service("fbpswtpthService")
	@SuppressWarnings({"unchecked"})
	public class FbpswtpthService implements IFbpswtpthService {
		private static String TX_NAME = "FBPSWHPTH";
		private LogUtils logUtils;

		@Autowired
		private IBisPathDao bisPathDao;
		@Autowired
		private IBisPathiDao bisPathiDao;
		@Autowired
		private IBisPathSwhDao bisPathSwhDao;
		@Autowired
		private IRetPrdInfoDao retPrdInfoDao;

		public String subMainProc(String evt_no,String strInMsg) {
			AppContext.clear();
			AppContext.setCurrServiceName(FbpswtpthService.class.getSimpleName());
			AppContext.setCurrEventNumber(evt_no);
			logUtils = new LogUtils(FbpswtpthService.class);
			logUtils.info("[InTrx:" + strInMsg + "]");
			
			String reply="";
			FbpswhpthI inTrx = new FbpswhpthI();
			FbpswhpthO outTrx = new FbpswhpthO();
			
			outTrx.setTrx_id(TX_NAME);
			outTrx.setType_id(TRX_OUT);
			outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
			try{
				inTrx = JacksonUtil.fromJson(strInMsg, FbpswhpthI.class);
				char action_flg = inTrx.getAction_flg().charAt(0);
				switch(action_flg){
					case ACTION_FLG_QUERY   :
						queryByCondition(inTrx,outTrx);
						break;
					case ACTION_FLG_ADD     :
						addFnc(inTrx,outTrx);
						break;
					case ACTION_FLG_UPDATE  :
						updateFnc(inTrx,outTrx);
						break;
					case ACTION_FLG_DELETE  :
						deleteFnc(inTrx,outTrx);
						break;
					case 'S' :
						queryAllSwhPath(inTrx,outTrx);
						break;
					default                 :
						errActionFnc(outTrx);
				}
			    if( RETURN_CODE_OK.equals(outTrx.getRtn_code()) ){
			    	bisPathSwhDao.flushCurSession();
				}else{
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				}		
			}catch(Exception ex){
				outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
				outTrx.setRtn_mesg(ex.toString());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}finally{
				reply=JacksonUtil.toJSONStr(outTrx);
				logUtils.info("[OutTrx:" + reply + "]");
			}
			return reply;
		}
		public void queryByCondition(FbpswhpthI inTrx,FbpswhpthO outTrx){
			FbpswhpthIA iary = inTrx.getIary().get(0);
			String prd_seq_id = iary.getPrd_seq_id();
			String hql ="FROM Bis_path_swh where 1=1 ";
			Bis_pathId bis_pathId = new Bis_pathId();
			if(null != prd_seq_id){
				Ret_prd_info ret_prd_info = retPrdInfoDao.get(prd_seq_id);
				if(ret_prd_info.getCr_path_id_fk()!=null){
					hql = hql + " and fr_path_id_fk ='" + ret_prd_info.getCr_path_id_fk() +"'";
				}
				if(ret_prd_info.getCr_path_ver_fk()!=null){
					hql = hql + " and fr_path_ver_fk='" + ret_prd_info.getCr_path_ver_fk()+"'";
				}
				if(ret_prd_info.getCr_ope_no_fk()!=null){
					hql = hql + " and fr_ope_no_fk='" + ret_prd_info.getCr_ope_no_fk()+"'";
				}
				if(iary.getSwh_typ()!=null){
					hql = hql +" and swh_typ='" + iary.getSwh_typ()+"'";
				}
				bis_pathId.setCr_ope_no(ret_prd_info.getCr_ope_no_fk());
				bis_pathId.setPath_id_fk(ret_prd_info.getCr_path_id_fk());
				bis_pathId.setPath_ver_fk(ret_prd_info.getCr_path_ver_fk());
			}else{
				if(iary.getFr_path_id_fk()!=null){
					hql = hql + " and fr_path_id_fk ='" + iary.getFr_path_id_fk() +"'";
				}
				if(iary.getFr_path_ver_fk()!=null){
					hql = hql + " and fr_path_ver_fk='" + iary.getFr_path_ver_fk()+"'";
				}
				if(iary.getFr_ope_no_fk()!=null){
					hql = hql + " and fr_ope_no_fk='" + iary.getFr_ope_no_fk()+"'";
				}
				if(iary.getSwh_id()!=null){
					hql = hql + " and swh_id='" + iary.getSwh_id()+"'";
				}
				if(iary.getSwh_typ()!=null){
					hql = hql +" and swh_typ='" + iary.getSwh_typ()+"'";
				}
				bis_pathId.setCr_ope_no(iary.getFr_ope_no_fk());
				bis_pathId.setPath_id_fk(iary.getFr_path_id_fk());
				bis_pathId.setPath_ver_fk(iary.getFr_path_ver_fk());
			}
			Bis_path bisPath = bisPathDao.get(bis_pathId);
			outTrx.setSwh_avl_flg(bisPath.getSwh_avl_flg());//当前站点的分支可用标识
			List<FbpswhpthOA> oaryList = new ArrayList<FbpswhpthOA>();
			List<Bis_path_swh> bisPathSwhList = bisPathSwhDao.find(hql);
			for(Bis_path_swh bis_path_swh:bisPathSwhList){
				FbpswhpthOA oary = new FbpswhpthOA();
				oary.setFr_ope_no_fk( bis_path_swh.getId().getFr_ope_no_fk());
				oary.setFr_path_id_fk( bis_path_swh.getId().getFr_path_id_fk());
				oary.setFr_path_ver_fk( bis_path_swh.getId().getFr_path_ver_fk());
				oary.setSwh_id( bis_path_swh.getId().getSwh_id());
				oary.setSwh_typ( bis_path_swh.getId().getSwh_typ());
				oary.setSwh_dsc( bis_path_swh.getSwh_dsc());
				oary.setSwh_path_id_fk( bis_path_swh.getSwh_path_id_fk());
				oary.setSwh_path_ver_fk( bis_path_swh.getSwh_path_ver_fk());
				oary.setOut_ope_no_fk( bis_path_swh.getOut_ope_no_fk());
				oary.setOut_path_id_fk( bis_path_swh.getOut_path_id_fk());
				oary.setOut_path_ver_fk( bis_path_swh.getOut_path_ver_fk());
				oaryList.add(oary);
			}
			outTrx.setOary(oaryList);
			outTrx.setTbl_cnt(oaryList.size());
			outTrx.setRtn_code(RETURN_CODE_OK);
		}
		public void addFnc(FbpswhpthI inTrx,FbpswhpthO outTrx){
			List<FbpswhpthIA> iaryList = inTrx.getIary();
			Timestamp currentTime = Util.getCurrentTimestamp();
			for(FbpswhpthIA iary : iaryList){
				Bis_path_swhId pathSwhId = new Bis_path_swhId();
				
				//检查swh_id长度，只能是0~~9之间的整数
				String swh_id = iary.getSwh_id();
				if (swh_id.length() != 1) {
					outTrx.setRtn_code(E_FBPSWHPTH_RECORD_ALREADY_EXIST+"");
					outTrx.setRtn_mesg("分支代码只能为0~~9之间的整数");
					return ;
				}
				
				int swh_id_int; 
				try {
					swh_id_int = Integer.valueOf(swh_id);
				} catch (NumberFormatException e) {
					outTrx.setRtn_code(E_FBPSWHPTH_RECORD_ALREADY_EXIST+"");
					outTrx.setRtn_mesg("分支代码只能为0~~9之间的整数");
					return ;
				}
				
				if (swh_id_int > 9 || swh_id_int < 0) {
					outTrx.setRtn_code(E_FBPSWHPTH_RECORD_ALREADY_EXIST+"");
					outTrx.setRtn_mesg("分支代码只能为0~~9之间的整数");
					return ;
				}
				
				iary.setSwh_id(String.valueOf(swh_id_int));
				
				pathSwhId.setFr_ope_no_fk(iary.getFr_ope_no_fk());
				pathSwhId.setFr_path_id_fk(iary.getFr_path_id_fk());
				pathSwhId.setFr_path_ver_fk(iary.getFr_path_ver_fk());
				pathSwhId.setSwh_id(iary.getSwh_id());
				pathSwhId.setSwh_typ(iary.getSwh_typ());
				Bis_path_swh bis_path_swh = bisPathSwhDao.get(pathSwhId);
				if(bis_path_swh!=null){
					outTrx.setRtn_code(E_FBPSWHPTH_RECORD_ALREADY_EXIST+"");
					outTrx.setRtn_mesg("该记录已经存在");
					return ;
				}
				//Check swh pathi exist or not
				if( bisPathiDao.getPathInfo(iary.getSwh_path_id_fk(),iary.getSwh_path_ver_fk())==null){
					outTrx.setRtn_code(E_FBPSWHPTH_SWH_PATH_NOT_EXIST+"");
					outTrx.setRtn_mesg("该分支线路["+iary.getSwh_path_id_fk()+"],["+iary.getSwh_path_ver_fk()+
							"]"+"不存在，请检查");
					return ;
				}
				
				//fr_path_id = out_path_id
				iary.setOut_path_id_fk(iary.getFr_path_id_fk());
				iary.setOut_path_ver_fk(iary.getFr_path_ver_fk());
				
				//check out path
				if(bisPathDao.getPathOpeInfo(iary.getOut_path_id_fk(), iary.getOut_path_ver_fk(), 
				   iary.getOut_ope_no_fk()) == null ){
					outTrx.setRtn_code(E_FBPSWHPTH_OUT_PATH_NOT_EXIST+"");
					outTrx.setRtn_mesg("out path["+iary.getOut_path_id_fk()+
							"],path_ver["+iary.getOut_path_ver_fk()+
							"],out_ope_no["+iary.getOut_ope_no_fk()+
							"] not exist,please check");
					return ;
					
				};
				bis_path_swh = new Bis_path_swh();
				bis_path_swh.setId(pathSwhId);
				bis_path_swh.setSwh_path_id_fk(iary.getSwh_path_id_fk());
				bis_path_swh.setSwh_path_ver_fk(iary.getSwh_path_ver_fk());
				bis_path_swh.setSwh_dsc(iary.getSwh_dsc());
				bis_path_swh.setOut_ope_no_fk(iary.getOut_ope_no_fk());
				bis_path_swh.setOut_path_id_fk(iary.getOut_path_id_fk());
				bis_path_swh.setOut_path_ver_fk(iary.getOut_path_ver_fk());
				bis_path_swh.setEvt_usr(inTrx.getEvt_usr());
				bis_path_swh.setEvt_timestamp(currentTime);
				bisPathSwhDao.save(bis_path_swh);
			}
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}
		public void updateFnc(FbpswhpthI inTrx,FbpswhpthO outTrx){
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}
		public void deleteFnc(FbpswhpthI inTrx,FbpswhpthO outTrx){
			List<FbpswhpthIA> iaryList = inTrx.getIary();
			for(FbpswhpthIA iary : iaryList){
				Bis_path_swhId pathSwhId = new Bis_path_swhId();
				pathSwhId.setFr_ope_no_fk(iary.getFr_ope_no_fk());
				pathSwhId.setFr_path_id_fk(iary.getFr_path_id_fk());
				pathSwhId.setFr_path_ver_fk(iary.getFr_path_ver_fk());
				
				int swh_id = 0;
				
				try {
					swh_id = Integer.valueOf(iary.getSwh_id());
				} catch (NumberFormatException e) {
					outTrx.setRtn_code(E_FBPSWHPTH_NOT_FOUND_RECORD+"");
					outTrx.setRtn_mesg("分支号不是整数!");
					return ;
				}
				
				pathSwhId.setSwh_id(String.valueOf(swh_id));
				pathSwhId.setSwh_typ(iary.getSwh_typ());
				Bis_path_swh bis_path_swh = bisPathSwhDao.get(pathSwhId);
				if(bis_path_swh==null){
					outTrx.setRtn_code(E_FBPSWHPTH_NOT_FOUND_RECORD+"");
					outTrx.setRtn_mesg("该分支信息没找到");
					return ;
				}
				//Check swh pathi exist or not
				bisPathSwhDao.delete(bis_path_swh);
			}
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}
		public void queryAllSwhPath(FbpswhpthI inTrx,FbpswhpthO outTrx){
			FbpswhpthIA iary = inTrx.getIary().get(0);
			String hql ="FROM Bis_path_swh where 1=1 ";
				if(iary.getFr_path_id_fk()!=null){
					hql = hql + " and fr_path_id_fk ='" + iary.getFr_path_id_fk() +"'";
				}
				if(iary.getFr_path_ver_fk()!=null){
					hql = hql + " and fr_path_ver_fk='" + iary.getFr_path_ver_fk()+"'";
				}
				if(iary.getFr_ope_no_fk()!=null){
					hql = hql + " and fr_ope_no_fk='" + iary.getFr_ope_no_fk()+"'";
				}
				if(iary.getSwh_id()!=null){
					hql = hql + " and swh_id='" + iary.getSwh_id()+"'";
				}
				if(iary.getSwh_typ()!=null){
					hql = hql +" and swh_typ='" + iary.getSwh_typ()+"'";
				}
			List<FbpswhpthOA> oaryList = new ArrayList<FbpswhpthOA>();
			List<Bis_path_swh> bisPathSwhList = bisPathSwhDao.find(hql);
			for(Bis_path_swh bis_path_swh:bisPathSwhList){
				FbpswhpthOA oary = new FbpswhpthOA();
				oary.setFr_ope_no_fk( bis_path_swh.getId().getFr_ope_no_fk());
				oary.setFr_path_id_fk( bis_path_swh.getId().getFr_path_id_fk());
				oary.setFr_path_ver_fk( bis_path_swh.getId().getFr_path_ver_fk());
				oary.setSwh_id( bis_path_swh.getId().getSwh_id());
				oary.setSwh_typ( bis_path_swh.getId().getSwh_typ());
				oary.setSwh_dsc( bis_path_swh.getSwh_dsc());
				oary.setSwh_path_id_fk( bis_path_swh.getSwh_path_id_fk());
				oary.setSwh_path_ver_fk( bis_path_swh.getSwh_path_ver_fk());
				oary.setOut_ope_no_fk( bis_path_swh.getOut_ope_no_fk());
				oary.setOut_path_id_fk( bis_path_swh.getOut_path_id_fk());
				oary.setOut_path_ver_fk( bis_path_swh.getOut_path_ver_fk());
				oaryList.add(oary);
			}
			outTrx.setOary(oaryList);
			outTrx.setTbl_cnt(oaryList.size());
			outTrx.setRtn_code(RETURN_CODE_OK);
		}

		public void errActionFnc(FbpswhpthO outTrx){
			outTrx.setRtn_code("0000009");
			outTrx.setRtn_mesg("Invalid Action_flg");
		}


	}